// pages/location/location.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    location:[

    ],
    payList:[]
  },
  // 删除地址
  clearLoca(event){
    let id = event.currentTarget.dataset.id

    let list = wx.getStorageSync('addresslists')
    let loca = list.filter(item=>{
      return item.id != id
    })
    

    this.setData({
      location:loca
    })
    wx.setStorageSync('addresslists',this.data.location);

  },
  // 保存地址前往支付
  goPay(){
      wx.navigateTo({
        url: "/pages/pay/pay",
      })
  },
  // 编辑地址
  editLoca(event){
    let id = event.currentTarget.dataset.id
    let list = wx.getStorageSync('addresslists')
    let loca = list.filter(item=>{
      return item.id == id
    })
    wx.setStorageSync('editLoca',loca[0]);

    wx.navigateTo({
      url: "/pages/address/address",
    })
  },
  // 勾选默认地址
  Checked(event){
    console.log(event.currentTarget.dataset.id);
    let index = event.currentTarget.dataset.id
    let list = wx.getStorageSync('addresslists')
    let checked = list[index].checked
    list[index].checked =!list[index].checked
    console.log(checked);
    console.log("是");


    for(var i = 0;i < list.length;i++){
      if(!list[index].checked){
        list[i].checked = true
        list[index].checked = false
      }
    }
    console.log(list);

    this.setData({
      location:list
    })
    wx.setStorageSync('addresslists',this.data.location);
  },
  // 手动添加地址
  address(){
    wx.navigateTo({
      url: "/pages/address/address",
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
  },

  /**
   * 生命周期函数--监听页面显示
   */

  onShow() {
    let paylist = wx.getStorageSync('payList')
    this.setData({
      payList:paylist
    })
    let list = wx.getStorageSync('address')
    console.log(list);
    let lists = wx.getStorageSync('addresslists')
    if(list){
      lists.push(list)
    }
    if(lists.length){
      this.setData({
        "location":lists
      })
    wx.setStorageSync('addresslists',this.data.location);
    wx.setStorageSync('address',"");
  }



  },


  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})